<?php
/*
 * 电子合同
 */
require_once dirname(dirname(__FILE__)) . '/include/common.inc.php';
require_once dirname(dirname(__FILE__)) . '/include/econtract.class.php';
require_once INCLUDE_ROOT_PATH . 'file_server.class.php';

$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : '';	//账户类型 0-个人，1-企业
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';	//账户类型 0-个人，1-企业
$mobile = isset($_REQUEST['mobile']) ? $_REQUEST['mobile'] : '';	//手机号
$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : '';	//名称、企业名称
$idcard = isset($_REQUEST['idcard']) ? $_REQUEST['idcard'] : '';	//身份证号、法人身份证号
$accountId = isset($_REQUEST['accountId']) ? $_REQUEST['accountId'] : '';	//生成的账户ID
$color = isset($_REQUEST['color']) ? $_REQUEST['color'] : '';	//印章颜色，1-红色，2-蓝色，3-黑色
$hText = isset($_REQUEST['hText']) ? $_REQUEST['hText'] : '';	//印章文字位置
$qText = isset($_REQUEST['qText']) ? $_REQUEST['qText'] : '';	//印章文字位置
$cust_id = isset($_REQUEST['cust_id']) ? $_REQUEST['cust_id'] : '';	//token验证
$filename = isset($_REQUEST['filename']) ? $_REQUEST['filename'] : '';	//token验证
$sealData = isset($_REQUEST['sealData']) ? $_REQUEST['sealData'] : '';	//token验证
$srcPdfFile = isset($_REQUEST['srcPdfFile']) ? $_REQUEST['srcPdfFile'] : '';	//token验证
$dstPdfFile = isset($_REQUEST['dstPdfFile']) ? $_REQUEST['dstPdfFile'] : '';	//token验证
$signType = isset($_REQUEST['signType']) ? $_REQUEST['signType'] : '';	//token验证
$posPage = isset($_REQUEST['posPage']) ? $_REQUEST['posPage'] : '';	//token验证
$posX = isset($_REQUEST['posX']) ? $_REQUEST['posX'] : '';	//token验证
$posY = isset($_REQUEST['posY']) ? $_REQUEST['posY'] : '';	//token验证
$key = isset($_REQUEST['key']) ? $_REQUEST['key'] : '';	//token验证
$token = isset($_REQUEST['token']) ? $_REQUEST['token'] : '';	//token验证
if ($token != md5($mobile . 'econtractupload'))//
{	

	$result = false;
}
else
{
	if(!file_exists(PDF_ROOT_PATH) && !is_dir(PDF_ROOT_PATH)){
		mkdir(PDF_ROOT_PATH); 
	}  
	$file_dir = PDF_ROOT_PATH . $cust_id . '/';
    $filepath = $file_dir . $filename;
    $srcPdf = $file_dir . $srcPdfFile;// 原始文件
    $first = $file_dir . $dstPdfFile;//第一次签章后文件
	$File_server = new File_server();
	$econtract = new Econtract();
	switch ($act)
	{
		case 'addAccount':
			$result = $econtract->addAccount($type, $mobile, $name, $idcard);
			break;

		case 'addTemplateSeal':
			$res = $econtract->addTemplateSeal($type, $accountId, $color, $hText, $qText);
            if($res['errCode'] == 0){
                $result = $res['sealData'];
            }else{
                $result = $res;
            }

			break;
		case 'userSign':
			$result = $econtract->userSign($accountId, $sealData, $srcPdf, $first, $fileName, $signType, $posPage, $posX, $posY, $key);

			break;
            
		case 'upload':
			$result = $File_server->upload($file_dir);
			break;
		default:
			$result = false;
			break;
	}
}


echo json_encode($result);
?>